var Main = {
    name: 'App',
    provide() {
        return {
            reload: this.reload
        }
    },
    data() {
        return {
            tableDrawer: false,
            detailDrawer: false,
            editDrawer: false,
            loading: false,
            isRouterAlive: true,
            form: {
                cornId: '-1',
                type: '1',
                jobGroup: '',
                jobName: '',
                invokeParam: '',
                quartzTime: '',
                status: '1',
                executeEndTime: '',
                createTime: '',
                rules: {
                    type: [{required: true, message: '请选择类型', trigger: 'blur'}],
                    jobName: [{required: true, message: '请输入任务名称', trigger: 'blur'}],
                    // quartzTime: [ { validator: this.validateCorn, trigger: 'blur' } ],
                    iphone: [
                        {required: true, message: '请输入手机号码', trigger: 'blur'},
                        {pattern: /^[1][3,4,5,6,7,8,9][0-9]{9}$/, message: '请输入正确格式的手机号码', trigger: 'blur'}
                    ],
                    idCard: [
                        {required: true, message: '请输入身份证号码', trigger: 'blur'},
                        {
                            pattern: /^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))((0[1-9])|([1-2][0-9])|(3[0-1]))\d{3}(\d|x|X)$/,
                            message: '请输入正确格式的身份证号码',
                            trigger: 'blur'
                        }
                    ]
                }
            },
            formLabelWidth: '80px',
            timer: null,
            resDate: [],
            taskData: [],
            today: '',
            inputJobName: ''
        }

    },
    created() {
        console.log('初始化')
        axios.get('/task/v1/query?day=null').then(result => {
            this.resDate = result.data.data
            this.dealMyDate();
        }).catch(error => {
            console.log(error.message)
        })
    },
    methods: {
        queryQuartz() {
            axios.post('/task/v1/queryMap', {
                'day': this.today,
                'jobName': this.inputJobName.trim()
            })
                .then(result => {
                    if (result.data.code === '00000') {
                        this.taskData = result.data.data
                    } else {
                        this.$message.error(result.data.msg);
                    }
                }).catch(error => {
                this.$message.error(error.message);
            })
        },
        getQuartzList(day) {
            this.today = day;
            axios.get('/task/v1/query?day=' + day).then(result => {
                this.taskData = result.data.data
            }).catch(error => {
                console.log(error.message)
            })
        },
        getQuartzOne(cornId) {
            if (cornId > 0) {
                axios.get('/task/v1/queryOne?cornId=' + cornId).then(result => {
                    result.data.data.type = result.data.data.type + '';
                    result.data.data.status = result.data.data.status + '';
                    this.form = result.data.data
                    console.log(" this.form==",  this.form)
                }).catch(error => {
                    this.$message.error(error.message);
                })
            } else {
                this.form = this.form
            }

        },
        saveOrUpdate() {
            axios.post('/task/v1/saveOrUpdate', this.form)
                .then(result => {
                    if (result.data.code === '00000') {
                        this.$message({
                            message: '保存成功',
                            type: 'success'
                        });
                        return true;
                    } else {
                        this.$message.error(result.data.msg);
                        return false;
                    }
                }).catch(error => {
                this.$message.error(error.message);
            })
        },
        removeQuartz(cornId) {
            this.$confirm('是否删除？')
                .then(_ => {
                    axios.get('/task/v1/remove?cornId=' + cornId).then(result => {
                        if (result.data.code === '00000') {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        } else {
                            this.$message.error(result.data.msg);
                        }
                    }).catch(error => {
                        this.$message.error(error.message);
                    })
                    this.timer = setTimeout(() => {
                        // 动画关闭需要一定的时间
                        setTimeout(() => {
                            this.getQuartzList(null)
                        }, 400);
                    }, 2000);
                })
                .catch(_ => {
                });
        },
        // 加载日历任务
        dealMyDate(v) {
            let len = this.resDate.length
            let htmlArry = []
            for (let i = 0; i < len; i++) {
                // 处理时间
                let time = this.resDate[i].createTime
                time = time.split(' ')[0]
                if (time == v) {
                    // htmlStr += '<div class="date-detail">'+this.resDate[i].content+'</div>‘
                    if (htmlArry.length < 1) {
                        htmlArry.push(this.resDate[i].jobName)
                    }

                }
            }
            return htmlArry
        },
        handleClose(done) {
            if (this.loading) {
                return;
            }
            this.$confirm('是否保存？')
                .then(_ => {
                    if (this.saveOrUpdate()) {
                        this.loading = true;
                        this.timer = setTimeout(() => {
                            done();
                            // 动画关闭需要一定的时间
                            setTimeout(() => {
                                this.loading = false;
                                this.getQuartzList(null)
                            }, 400);
                        }, 2000);
                    }
                })
                .catch(_ => {
                });
        },
        handleEditClose(done) {
            if (this.loading) {
                return;
            }
            this.$confirm('是否保存？')
                .then(_ => {
                    if (this.saveOrUpdate()) {
                        this.loading = true;
                        this.timer = setTimeout(() => {
                            done();
                            // 动画关闭需要一定的时间
                            setTimeout(() => {
                                this.loading = false;
                                this.reload()
                            }, 400);
                        }, 2000);
                    }
                })
                .catch(_ => {
                });
        },
        cancelForm() {
            this.loading = false;
            this.detailDrawer = false;
            clearTimeout(this.timer);
        },
        cancelEditForm() {
            this.loading = false;
            this.editDrawer = false;
            clearTimeout(this.timer);
        },
        validateCorn(rule, value, callback) {
            if (cron.isValidCron(value) === true) {
                callback()
            } else {
                callback(new Error('请输入正确的Corn表达式'))
            }
        },
        typeFormatter(row, column, cellValue) {
            return row.type === '1' ? '常规' : '特殊'
        },
        statusFormatter(row, column, cellValue) {
            return row.status === '1' ? '待执行' : '已执行'
        },
        reload() {
            this.isRouterAlive = false
            this.$nextTick(() => this.isRouterAlive = true)
        }
    }

}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')